﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using System.Web.Razor;

namespace RazorTemplateEngine
{
    public class TemplatePageRazorHost : System.Web.Razor.RazorEngineHost
    {
        public const string BaseClassConst = "__TemplatePage";
        public const string ClassNameConst = "__TemplateInherit";
        public const string NamespanceConst = "RazorTemplateEngine";


        public TemplatePageRazorHost(RazorCodeLanguage codeLanguage) : base(codeLanguage)
        {
            baseClass = "__TemplatePage";
        }

        private string baseClass;

        public override string DefaultBaseClass
        {
            get
            {
                return baseClass;
            }
            set
            {
                baseClass = value;
            }
        }

        public override string DefaultClassName
        {
            get
            {
                return ClassNameConst;
            }
            set
            {
                base.DefaultClassName = value;
            }
        }

        public override string DefaultNamespace
        {
            get
            {
                return NamespanceConst;
            }
            set
            {
                base.DefaultNamespace = value;
            }
        }


        public override void PostProcessGeneratedCode(CodeCompileUnit codeCompileUnit, CodeNamespace generatedNamespace, CodeTypeDeclaration generatedClass, CodeMemberMethod executeMethod)
        {
            base.PostProcessGeneratedCode(codeCompileUnit, generatedNamespace, generatedClass, executeMethod);
            generatedNamespace.Imports.Add(GetBaseClassNamepace());
            ImportNamespaceResolver namespaceResolver = new ImportNamespaceResolver();
            foreach (Namespace namespaceConfig in namespaceResolver.GetConfigNamespaces())
                generatedNamespace.Imports.Add(new CodeNamespaceImport(namespaceConfig.NamespaceName));
        }

        private CodeNamespaceImport GetBaseClassNamepace()
        {
            return new CodeNamespaceImport(typeof(__TemplatePage).Namespace);
        }

    }
}
